home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Vector / Base_Vector.h < prev    next >
C/C++ Source or Header  |  1992-06-19  |  8KB  |  188 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 08/31/89 -- Initial design and implementation
  13. // Updated: MBN 10/03/89 -- Predecrement instead of postdecrement on prev()
  14. // Updated: MBN 10/10/89 -- Added current_position() method for Iterator<Type>
  15. // Updated: VDN 02/21/92 -- New lite version
  16. //
  17. // The Base_Vector class is no longer derived from the Generic class and is used to
  18. // implement non-type specific functionality for the parameterized CoolVector<Type>
  19. // class. In this manner, code common to all instances of  the Base_Vector class can
  20. // be shared to reduce code  replication. The CoolVector<Type>  class is dynamic in
  21. // the sense that an object can grow dynamically if necessary.  The growth size
  22. // is determined by the  value  of a  static  allocation size variable  for the
  23. // class.  However, fixed length Base_Vectors  are also  supported by  setting  this
  24. // variable  to INVALID.   The Base_Vector class  implements the notion of a current
  25. // position. This is useful  for iterating through  the  elements of a  Base_Vector.
  26. // The current position is maintained in an integer and  is set or reset by all
  27. // methods  affecting elements in the  Base_Vector class. Methods to reset,  move to
  28. // the next and previous, find,  and get the value at  the current position are
  29. // provided.
  30. // 
  31. // Each Base_Vector object contains a protected data section that has a slot to hold
  32. // the current  size  of the  Base_Vector and a pointer  to an allocated block large
  33. // enough to hold "size" elements of type "Type". A  slot to hold the number of
  34. // elements is also provided. A single protected  slot contains  a pointer to a
  35. // compare  function to be  used in equality operations.  The  default function
  36. // used is the built-in == operator.  Finally, a current  position slot is used
  37. // to maintain the index of the last element affected by an operation.
  38. //
  39. // Three different constructors are  provided.  The first constructor  takes no
  40. // arguments and creates an empty Base_Vector object. The second constructor takes a
  41. // single argument specifying  the  initial  size  of  the  Base_Vector.   The third
  42. // constructor takes takes a  single argument consisting of  a  reference  to a
  43. // Base_Vector and duplicates its size and element values.
  44. // 
  45. // Methods are provided  to  destructively   perform reverse, remove,  replace,
  46. // prepend, append, merge, sort, and insert operations on a Base_Vector object.   In
  47. // addition, several miscelaneous methods  support  resize, copy, fill, length,
  48. // search,  push, push_new, pop,  and   position functions.  The assignment and
  49. // element accessor functions allow for individual elements of the Base_Vector to be
  50. // set and read.  Finally, both equality and non-equality tests are implemented
  51. // via  a user-defined  comparison function  as  well as  overloaded input  and
  52. // output operators.
  53. //
  54.  
  55. #ifndef BASE_VECTORH            // If no definition for Base_Vector
  56. #define BASE_VECTORH            // define the Base_Vector symbol
  57.  
  58. #ifndef STREAMH            // If the Stream support not yet defined,
  59. #if defined(DOS) || defined(M_XENIX)
  60. #include <stream.hxx>           // include the Stream class header file
  61. #else
  62. #include <stream.h>        // include the Stream class header file
  63. #endif
  64. #define STREAMH
  65. #endif
  66.  
  67. #ifndef MISCELANEOUSH        // If we have not included this file,
  68. #include <misc.h>        // include miscelaneous useful definitions.
  69. #endif
  70.  
  71.  
  72. #define MEM_BLK_SZ 100
  73.  
  74. typedef long CoolVector_state;            // Current position state
  75.  
  76. class CoolVector {
  77. public:
  78.   inline void reset ();                // Invalidate current position
  79.   inline Boolean next ();            // Advance current position
  80.   inline Boolean prev ();            // Backup current position 
  81.   inline long position () const;        // Return current position
  82.   inline CoolVector_state& current_position ();// Set/Get current position
  83.  
  84.   CoolVector& operator= (const CoolVector&);    // Overload assignment operator
  85.   void clear();                    // Removes all elements
  86.   Boolean is_empty();                // Any elements in Vector?
  87.   inline long length () const;            // Number of elements
  88.   inline long capacity () const;        // Maximum number of elements
  89.  
  90.   long set_length (long, const char*);        // Set number of elements
  91.   void set_growth_ratio (float, const char*);    // Set growth percentage
  92.   void set_alloc_size (int, const char*);    // Set alloc size
  93.  
  94. protected:
  95.   long size;                    // Size of allocated storage 
  96.   long number_elements;                // Number of elements in CoolVector
  97.   int alloc_size;                // Allocation size for growth
  98.   static float growth_ratio;            // If non-zero, growth ratio 
  99.   CoolVector_state curpos;                // Keeps current position
  100.  
  101.   void bracket_error (const char*, const long) const;   // Raise exception
  102.   void value_error (const char*) const;                // Raise exception
  103.   void resize_error (const char*, const long) const;    // Raise exception
  104.   void static_error (const char*) const;        // Raise exception
  105.   void assign_error (const char*) const;        // Raise exception
  106.   void fill_start_error(const char*,const long) const;    // Raise exception
  107.   void fill_end_error (const char*, const long) const;    // Raise exception
  108.   void copy_start_error(const char*,const long) const;    // Raise exception
  109.   void copy_end_error (const char*, const long) const;    // Raise exception
  110.   void copy_error (const char*);            // Raise exception
  111.   void remove_error (const char*) const;        // Raise exception
  112.   void va_arg_error (const char*, int);        // Raise exception
  113.  
  114.   CoolVector ();                // CoolVector v; 
  115.   CoolVector (long);                // CoolVector v(10);
  116.   CoolVector (const CoolVector&);        // CoolVector v = y;
  117.   ~CoolVector ();                // Destructor
  118. };
  119.  
  120.  
  121. // void reset () -- Set current position to INVALID.
  122. // Input:           None
  123. // Output:          None
  124.  
  125. inline void CoolVector::reset () {
  126.   this->curpos = INVALID;            // Invalidate current position
  127. }
  128.  
  129.  
  130. // Boolean next () -- Increment current position. If INVALID, set to first.
  131. // Input:             None
  132. // Output:            TRUE/FALSE
  133.  
  134. inline Boolean CoolVector::next () {
  135.   return ((++this->curpos >= this->number_elements) ?
  136.       (this->curpos = INVALID, FALSE) : TRUE);
  137. }
  138.     
  139.  
  140. // Boolean prev () -- Decrement current position. If INVALID, set to last.
  141. // Input:             None
  142. // Output:            TRUE/FALSE
  143.  
  144. inline Boolean CoolVector::prev () {
  145.   if (this->curpos == INVALID)            // If INVALID current position
  146.     this->curpos = this->number_elements;    // Set to last element
  147.   return (--this->curpos >= 0);            // Else decrement and return
  148. }
  149.  
  150.  
  151. // long length () -- Return the number of elements in a CoolVector object.
  152. // Input:            None
  153. // Output:           Integer representing number of elements
  154.  
  155. inline long CoolVector::length () const {
  156.   return (this->number_elements);
  157. }
  158.  
  159.  
  160. // long capacity () -- Return maximum number of elements CoolVector obj can hold.
  161. // Input:              None
  162. // Output:             Integer value of maximum number of elements
  163.  
  164. inline long CoolVector::capacity () const {
  165.   return (this->size);                // Maximum number of elements
  166. }
  167.  
  168.  
  169. // long position () -- Return current position.
  170. // Input:              None
  171. // Output:             Integer representing current position
  172.  
  173. inline long CoolVector::position () const {
  174.   return this->curpos;
  175. }
  176.  
  177.  
  178. // current_position () -- Return current position state
  179. // Input:                 None
  180. // Output:                Reference to current position state
  181.  
  182. inline CoolVector_state& CoolVector::current_position () {
  183.   return this->curpos;
  184. }
  185.  
  186. #endif                        // End of BASE_VECTORH
  187.  
  188.